<!--
 * @Author: wangjh wangjiahui0710@163.com
 * @Date: 2024-07-10 09:19:29
 * @LastEditors: wangjh wangjiahui0710@163.com
 * @LastEditTime: 2024-07-15 15:10:13
 * @FilePath: \cip-admin\src\pages\company\information\third-party-company\components\detail-dialog.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<!-- 弹窗 详情 -->
<script lang="ts" setup>
import type { FormInstance, FormRules } from "element-plus";
import { whManagerBsTbThirdUnitUploadSafeEduRecordUsingPost } from "@/genapi/wh";

import { useAsync } from "@/hooks/use-async";
import { useCompanyStore } from "@/stores/company";
import { injectState } from "../state";

const { page } = injectState()!;

defineOptions({ name: "DetailDialog" });

const companyStore = useCompanyStore();

const dialogRef = shallowRef<InstanceType<GlobalComponents["PromiseDialog"]>>();
const formRef = shallowRef<FormInstance>();

const dialogType = ref<"add" | "edit" | "detail" | string>();

const titleMap: Record<string, string> = {
	add: "新增",
	edit: "编辑",
	detail: "详情",
};

const beforeClose = async () => {
	if (dialogType.value !== "detail") {
		await ElMessageBox.confirm("确认关闭？");
	}
};

const trigger = async (type = "add", value = {} as any) => {
	dialogType.value = type;
	model.value = JSON.parse(JSON.stringify(value)) as any;
	await dialogRef.value?.open();
};

defineExpose({
	trigger,
});

const model = ref<Record<string, any>>({
  fileIdList:[],
  photoIdList:[]
});

const rules: FormRules<typeof model.value> = {
	xxx: { required: true, message: "企业名称不可为空", trigger: "blur" },
};

const { execute, isLoading } = useAsync(async () => {
	await formRef.value?.validate();
	await ElMessageBox.confirm("确认提交？", "提示", {
		type: "warning",
	});
	await whManagerBsTbThirdUnitUploadSafeEduRecordUsingPost({
		data: {
			trainContent: model.value.trainContent,
			trainTime: model.value.trainTime,
			enterpriseId: companyStore.currentCompanyId,
			thirdUnitId: model.value.id,
		},
	});
	ElMessage.success("提交成功");
	page.execute();
	dialogRef.value?.complete();
});
</script>

<template>
	<PromiseDialog
		ref="dialogRef"
		:title="titleMap[dialogType!]"
		:before-close="beforeClose"
		max-h="884px!"
		w="707px!"
	>
		<el-scrollbar>
			<el-form
				ref="formRef"
				:disabled="dialogType === 'detail'"
				:model="model"
				:rules="rules"
				inline
				label-position="top"
			>
				<el-form-item label="培训时间" prop="培训时间" w="100%!">
					<el-date-picker
						v-model="model['trainTime']"
						class="w-100%!"
						value-format="YYYY-MM-DD"
						clearable
						placeholder="请输入培训时间"
					/>
				</el-form-item>
				<el-form-item label="培训内容" prop="培训内容" w="100%!">
					<el-input
						v-model="model['trainContent']"
						placeholder="请输入培训内容"
					/>
				</el-form-item>
				<el-form-item label="附件信息" prop="附件信息" w="100%!">
          <a download :href=" item.path" v-for="item in model.fileIdList" :key="item.id">{{ item.name }}</a>
					<FileUpload v-model="model['fileIdList']" v-show="dialogType !== 'detail'" />
				</el-form-item>
				<el-form-item label="上传图片" prop="上传图片" w="100%!">
					<FileUpload
						v-model="model['photoIdList']"
						list-type="picture-card"
					></FileUpload>
				</el-form-item>
			</el-form>
		</el-scrollbar>
		<template v-if="dialogType !== 'detail'" #footer>
			<el-button @click="dialogRef?.close()">取消</el-button>
			<el-button type="primary" :is-loading="isLoading" @click="execute()"
				>确认</el-button
			>
		</template>
	</PromiseDialog>
</template>
